package com.android.datn.activities;

import java.io.File;

import com.android.datn.R;
import com.android.datn.R.layout;
import com.android.datn.R.menu;
import com.android.datn.activities.offline.GActivityOffline;
import com.android.datn.customs.view.SlideHolder;
import com.android.datn.daos.TrackDAO;
import com.android.datn.helper.DBBinding;
import com.android.datn.services.AudioPlayer;

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends TabActivity implements OnClickListener {
	private TextView mTvAnimation;
	private SlideHolder mSlideHolder;
	private ImageView mImgHome;
	private Intent mAudioPlayerIntent;
	private Button mBtnProfile, mBtnFavorite, mBtnRecent, mBtnLogout, mBtnCurentLocation;
	private TrackDAO mTrackDAO;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initTabs();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		Toast.makeText(getBaseContext(), "Exited", Toast.LENGTH_SHORT).show();
		stopService(mAudioPlayerIntent);
		mTrackDAO.deleteTable(DBBinding.DB_VOTE);
		super.onDestroy();
	}

	private void initTabs() {
		mTrackDAO = TrackDAO.getInstance();
		mAudioPlayerIntent = new Intent(this, AudioPlayer.class);
		startService(mAudioPlayerIntent);
		mTvAnimation = (TextView) findViewById(R.id.tv_animation);
		mTvAnimation.setAnimation(scrollingText(mTvAnimation, 10));
		addTab(R.drawable.btn_home, GActivityHome.class, R.layout.tab_item,
				"Home");
		addTab(R.drawable.btn_top, GActivityTop.class, R.layout.tab_item, "Top");
		addTab(R.drawable.btn_offline, GActivityOffline.class,
				R.layout.tab_item, "Offline");
		addTab(R.drawable.btn_news, GActivityNews.class, R.layout.tab_item,
				"News");
		mSlideHolder = (SlideHolder) findViewById(R.id.slideHolder);
		mImgHome = (ImageView) findViewById(R.id.img_home);
		mImgHome.setOnClickListener(this);
		mBtnFavorite = (Button) findViewById(R.id.btn_favorite);
		mBtnLogout = (Button) findViewById(R.id.btn_logout);
		mBtnProfile = (Button) findViewById(R.id.btn_profile);
		mBtnRecent = (Button) findViewById(R.id.btn_recent);
		mBtnCurentLocation = (Button) findViewById(R.id.location_current);
		mBtnCurentLocation.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				/*File file = new File("/data/data/"
						+ getApplicationContext().getPackageName()
						+ "/databases/" + DBBinding.DB_NAME);
					if (file.exists()) {
						Toast.makeText(getBaseContext(), "Exists", Toast.LENGTH_SHORT).show();
					}
					else 
						Toast.makeText(getBaseContext(), "None", Toast.LENGTH_SHORT).show();
					file.delete();*/
			}
		});
		mBtnFavorite.setOnClickListener(this);
		mBtnLogout.setOnClickListener(this);
		mBtnProfile.setOnClickListener(this);
		mBtnRecent.setOnClickListener(this);
	}

	private void addTab(int drawableId, Class<?> c, int layoutID, String title) {
		TabHost tabHost = getTabHost();
		Intent intent = new Intent(this, c);
		TabHost.TabSpec spec = tabHost.newTabSpec("tab" + drawableId);
		View tabIndicator = LayoutInflater.from(this).inflate(layoutID,
				getTabWidget(), false);
		ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
		TextView text = (TextView) tabIndicator.findViewById(R.id.text_below);
		icon.setImageResource(drawableId);
		text.setText(title);
		spec.setIndicator(tabIndicator);
		spec.setContent(intent);
		tabHost.addTab(spec);
	}

	public Animation scrollingText(View view, float margin) {

		Context context = view.getContext(); // gets the context of the view

		// measures the unconstrained size of the view
		// before it is drawn in the layout
		view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

		// takes the unconstrained wisth of the view
		float width = view.getMeasuredHeight();
		// gets the screen width
		float screenWidth = ((Activity) context).getWindowManager()
				.getDefaultDisplay().getWidth();
		// perfrms the calculation
		float toXDelta = width - (screenWidth - margin);

		// sets toXDelta to 0 if the text width is smaller that the screen size
		if (toXDelta < 0) {
			toXDelta = 0;
		} else {
			toXDelta = 0 - toXDelta;
		}

		// Animation parameters
		Animation mAnimation = new TranslateAnimation(0, toXDelta, 0, 0);
		mAnimation.setDuration(15000);
		mAnimation.setRepeatMode(Animation.RESTART);
		mAnimation.setRepeatCount(Animation.INFINITE);

		return mAnimation;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.img_home:
			mSlideHolder.toggle();
			break;
		case R.id.btn_favorite:
			Intent intent = new Intent(this, ASongOfFavorite.class);
			startActivity(intent);
			break;
		case R.id.btn_logout:
			break;
			
		case R.id.btn_profile:
			break;
		case R.id.btn_recent:
			break;
		default:
			break;
		}
	}
}
